package com.lcz.authority.common.domain.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lcz.authority.common.annotation.Excel;
import com.zhou.starter.jpacomment.annotation.ColumnComment;
import com.zhou.starter.jpacomment.annotation.TableComment;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lcz
 * @desc 系统访问记录表
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */
@Data
@TableName("sys_log_login")
@Table(name = "sys_log_login")
@Entity
@TableComment("系统访问记录表")
public class SysLoginLogEntity implements Serializable {
    private static final Integer PAGE_NUM = 1;
    private static final Integer PAGE_SIZE = 10;
    /**
     * ID
     */
    @Id
    @TableId(type = IdType.ASSIGN_ID)
    @ColumnComment("主键id")
    private String infoId;
    /**
     * 登录成功的用户id
     */
    @ColumnComment("登录成功的用户id")
    private String userId;
    /**
     * 用户账号
     */
    @Excel(name = "用户账号")
    @ColumnComment("用户账号")
    private String userName;
    /**
     * 登录状态 0成功 1失败
     */
    @Excel(name = "登录状态", readConverterExp = "0=成功,1=失败")
    @ColumnComment("登录状态 0成功 1失败")
    private String status;
    /**
     * 登录IP地址
     */
    @Excel(name = "登录地址")
    @ColumnComment("登录IP地址")
    private String ipaddr;
    /**
     * 登录地点
     */
    @Excel(name = "登录地点")
    @ColumnComment("登录地点")
    private String loginLocation;
    /**
     * 浏览器类型
     */
    @Excel(name = "浏览器")
    @ColumnComment("浏览器")
    private String browser;
    /**
     * 操作系统
     */
    @Excel(name = "操作系统")
    @ColumnComment("操作系统")
    private String os;
    /**
     * 提示消息
     */
    @Excel(name = "提示消息")
    @ColumnComment("提示消息")
    private String msg;
    /**
     * 访问时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Excel(name = "访问时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    @ColumnComment("访问时间")
    private Date loginTime;
    /**
     * 请求参数
     */
    @TableField(exist = false)
    @Transient
    private Map<String, Object> params;

    @NotNull(message = "页码不能为空")
    @Min(value = 1, message = "页码最小值为 1")
    @TableField(exist = false)
    @Transient
    @JsonIgnore
    private Integer pageNum = PAGE_NUM;

    @NotNull(message = "每页条数不能为空")
    @Min(value = 1, message = "每页条数最小值为 1")
    @Max(value = 100, message = "每页条数最大值为 100")
    @TableField(exist = false)
    @Transient
    @JsonIgnore
    private Integer pageSize = PAGE_SIZE;

    public Map<String, Object> getParams() {
        if (params == null) {
            params = new HashMap<>();
        }
        return params;
    }
}
